<?php

namespace App\Http\Controllers\home;

use App\Price;
use App\Type;
use App\User_venue;
use App\Order;
use App\Venue;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class VenueController extends Controller
{
    //获取场馆列表
    public function list(){

        $types = Type::all();
        $venues = Venue::orderBy("id","desc")->paginate(6);
        return view("venue/list",compact("venues","types"));

    }

    //获取栏目下的场馆列表
    public function type($id){

        $types = Type::all();
        $venues = Venue::where("type_id",$id)->orderBy("id","desc")->paginate(6);
        return view("venue/list",compact("venues","types"));

    }


    //获取场馆的详情
    public function detail($id){
        $types = Type::all();
        $venue = Venue::find($id);
        $dates = $this->get_dates();
        $times = $this->get_times();
        $date_sn_time_prices = $this->date_sn_time_prices($id);
        return view("venue/detail",compact("venue","types","dates","times","date_sn_time_prices"));
    }

    // 获取每天对应的价格数据 date_sn_time_prices

    private function date_sn_time_prices($venue_id){

        // 获取一周的日期
        $dates = $this->get_dates();
        $date_list = Array();

        for($i=0; $i<count($dates); $i++){
            $order_date = $dates[$i]["date"];
            $date_list[$order_date] = $this->sn_time_prices($venue_id,$order_date);
        }

        /*
        $date_list["2019-7-5"] = Array(
            1=>Array(9=>45,10=>45,18=>60,19=>60,22=>60),
            2=>Array(9=>45,10=>45,18=>60,19=>60,22=>60),
            3=>Array(9=>45,10=>45,18=>60,19=>60,22=>60)
        );
        */

        return $date_list;

    }

    // 获取当前场馆 不同场地 时间价格列表
    private function  sn_time_prices($venue_id,$order_date){
        $sn_time_prices = Array();

        $venue = Venue::find($venue_id);

        $num = $venue->num;
        for($i=1;$i<=$num;$i++){
            $sn = $i;
            $sn_time_prices[$sn] = $this->time_prices($venue_id,$order_date,$sn);
        }
        return $sn_time_prices;
    }

    // 获取每块场地  时段价格列表

    private function time_prices($venue_id,$order_date,$sn){

        //  3=>Array(9=>45,10=>45,18=>60,19=>60,22=>60);

        $time_prices = Array();

        $prices = Price::where("venue_id",$venue_id)->get();

        $prices = $prices->toArray();

         for($i=0;$i<count($prices);$i++){
             $time = $prices[$i]["time"];
             $price = $prices[$i]["price"];

             $time_prices[$time]=Array();  // Array("price"=>45,"is_ordered"=>true)
             $time_prices[$time]["price"] = $price;
             $time_prices[$time]["is_ordered"] = $this->check_orders($venue_id,$order_date,$sn,$time);
         }

         return $time_prices;


    }

    // 判断当前时段是否被预定
    private function check_orders($venue_id,$order_date,$sn,$time){

           return (bool) Order::where("venue_id",$venue_id)
               ->where("order_date",$order_date)
               ->where("sn",$sn)
               ->where("time",$time)
               ->first();

    }


    // 获取从今天开始一周的日期和星期
    private function get_dates(){

      $dates = Array();
      $week= Array("日","一","二","三","四","五","六");

      for($i=0;$i<=6;$i++){
          $dates[$i]["date"] =  date("Y-m-d",time()+ 24*60*60*$i);
          $sn = date("w",time()+ 24*60*60*$i);  // 0 ~ 6
          $dates[$i]["week"] = "星期".$week[$sn];
      }

      return $dates;

    }


    // 获取所有的时段
    private function get_times(){
        $times = Array();
        for($i=9;$i<=22;$i++){
            $times[]=$i;
        }
        return $times;
    }


    //创建场馆
    public function create(){
        $types = Type::all();
        return view("venue/create",compact("types"));
    }

    //我的场馆
    public function my_venues(){
        $venues = Venue::where("user_id",\Auth::id())->get();
        return view("venue/my_venues",compact("venues"));
    }


    // 修改场馆
    public function edit($id){
        $venue = Venue::find($id);
        $types = Type::all();
        return view("venue/edit",compact("venue","types"));
    }

    // 删除场馆
    public function delete($id){
        $venue = Venue::find($id);
        $venue->delete();
        $venues = Venue::where("user_id",\Auth::id())->get();
        return view("venue/my_venues",compact("venues"));
    }

    //保存场馆
    public function save(Request $request){
       // dump
        $data = $request->except("_token");

        $data["user_id"] = \Auth::id();
        $data["pics"] = "img_01.jpg,img_02.jpg,img_03.jpg";

        $res = Venue::create($data);

        $this->init_price($res->id);

        return redirect("venue/my_venues");

    }

    private  function init_price($id){

        for($i=9;$i<23;$i++){
            $price = new Price();
            $price->venue_id = $id;
            $price->time = $i;
            $price->price = 45;
            $price->save();

        }

    }

    //保存场馆
    public function update(Request $request){
        // dump
        $data = $request->except("_token");

        $data["user_id"] = \Auth::id();
        $data["pics"] = "img_01.jpg,img_02.jpg,img_03.jpg";

        $venue = Venue::find($request->id);

        $venue->update($data);

        return redirect("venue/my_venues");

    }

    // 关注场馆
    public function follow($id){

        // 1 user_id
        // 2 venue_id
        // 3 向  venue_user 追加记录

        /*
        $user = \Auth::user();
        $user_venue = new User_venue();
        $user_venue->user_id = $user->id;
        $user_venue->venue_id = $id;
        $user_venue->save();
        */

        // $user->venues;    没有括号 数据 当前用户关注的所有场馆
        // $user->venues();  关联关系 对象 attach() detach()


       $user = \Auth::user();

       $user->venues()->attach($id);  // 关联登录用户 与传入的场馆id

       return back();


    }

    public function unfollow($id){

        $user = \Auth::user();
        $user->venues()->detach($id);
        return back();

    }


    // 我的收藏
    public function my_follows(){

        $venues = \Auth::user()->venues;  // 不加小括号  数据


        return view("venue/my_follows",compact("venues"));
    }


}
